﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports SecurityService

Partial Class Control_Announcement_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            Dim ctrl As System.Web.UI.WebControls.WebControl
            ctrl = txtContent.CreateCommandButton("ask_mask_fullname", "remove_format.gif", "Bỏ định dạng")
            ctrl.Attributes("onclick") = "removeTextFormat('" + txtContent.ClientID + "');"
            'get the pos after the Italic    
            Dim pos As Integer
            pos = txtContent.ToolControls.IndexOf("JustifyRight") + 1
            'add this custom button into the editor    
            txtContent.InsertToolControl(pos, "ask_mask_fullname", ctrl)
            If Not Request("Id") Is Nothing Then
                BindAnnouncement(Request("Id"))
            End If



        End If
    End Sub
#Region "Event for control "
    Protected Sub BindAnnouncement(ByVal _id As Integer)
        Using data As New VSEntities
            Dim p = (From q In data.Announcements Where q.Id = _id Select q).FirstOrDefault
            If Not p Is Nothing Then
                txtTitle.Text = p.Title
                txtDescription.Text = p.Description
                txtContent.Text = p.Contents
                hidImagePath.Text = p.FileAttach
                chkIsNew.Checked = p.IsNew
            End If

        End Using

    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities

            Try
                Dim _id As Integer = Request("Id")

                Dim p = (From q In data.Announcements Where q.Id = _id Select q).FirstOrDefault
                If Not p Is Nothing Then
                    p.Title = txtTitle.Text.Trim()
                    p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
                    p.Description = txtDescription.Text.Trim()
                    p.Contents = txtContent.Text.Trim()
                    p.Modified = Now()
                    p.Modifier = Session("UserName")
                    p.FileAttach = hidImagePath.Text.ToString
                    p.IsNew = IIf(chkIsNew.Checked = True, True, False)
                    data.SaveChanges()
                    Insert_App_Log("Edit Announcement:" & txtTitle.Text.Trim & "", Function_Name.Announcement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Announcement/List.aspx';", Me.Page, True)

                End If



            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub



#End Region
End Class
